using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using BENBUN.Properties;

namespace BENBUN.Settings
{
    internal class Settings
    {
        private static readonly string prefsPath = (Environment.GetEnvironmentVariable("APPDATA") + "/" + Resources.StringResources.PrefDirectory + "/preferences.xml");
        private static string _twitterScreenName;
        private static string _twitterPassword;
        private static bool _twitterSTTag = true;
        private static bool _twitterCustomTagEnabled = false;
        private static string _twitterCustomTag;

        private static string _twitterTotalTags = "";
        private static int _twitterTweetCapacity = 0;

        static Settings()
        {
            if (!load())
            {
                Directory.CreateDirectory(Environment.GetEnvironmentVariable("APPDATA") + "/\\" + Resources.StringResources.PrefDirectory);
                reset();
                save();
            }
        }

        public static void save()
        {
            XmlDocument document = new XmlDocument();
            XmlElement newChild = document.CreateElement("ST_Twitter");
            document.AppendChild(newChild);
            XmlElement element2 = document.CreateElement("settings");
            newChild.AppendChild(element2);
            element2.SetAttribute("twitterName", _twitterScreenName);
            element2.SetAttribute("twitterPassword", _twitterPassword);

            element2.SetAttribute("twitterSTTag", XmlConvert.ToString(_twitterSTTag));

            element2.SetAttribute("twitterCustomTagEnabled", XmlConvert.ToString(_twitterCustomTagEnabled));
            element2.SetAttribute("twitterCustomTag", _twitterCustomTag);

            XmlTextWriter w = new XmlTextWriter(prefsPath, Encoding.UTF8);
            w.Formatting = Formatting.Indented;
            w.Indentation = 3;
            w.IndentChar = ' ';
            document.WriteContentTo(w);
            w.Close();

            setTags();
        }

        private static bool load()
        {
            if (!File.Exists(prefsPath))
            {
                return false;
            }
            XmlDocument document = new XmlDocument();
            XmlReader reader = new XmlTextReader(prefsPath);
            document.Load(reader);
            //
            // Read in the original settings
            //
            try
            {
                XmlNode node = document.ChildNodes[0]["settings"];
                _twitterScreenName = node.Attributes["twitterName"].Value;
                _twitterPassword = node.Attributes["twitterPassword"].Value;

                _twitterSTTag = XmlConvert.ToBoolean(node.Attributes["twitterSTTag"].Value);

                _twitterCustomTagEnabled = XmlConvert.ToBoolean(node.Attributes["twitterCustomTagEnabled"].Value);
                _twitterCustomTag = node.Attributes["twitterCustomTag"].Value;

            }
            catch (Exception)
            {
                reader.Close();
                return false;
            }
            reader.Close();

            setTags();
            return true;
        }


        public static void reset()
        {
            _twitterScreenName = "";
            _twitterPassword = "";
        }

        private static void setTags()
        {
            string totalTags;

            totalTags = " " + BENBUN.Properties.Resources.TwitterTag;

            if (_twitterSTTag)
            {
                totalTags = totalTags + " " + BENBUN.Properties.Resources.TwitterTagST;
            }

            if (_twitterCustomTagEnabled)
            {
                totalTags = totalTags + " " + _twitterCustomTag;
            }

            _twitterTotalTags = totalTags;
            _twitterTweetCapacity = 140 - totalTags.Length;

        }

        public static string TwitterTotalTags
        {
            get { return _twitterTotalTags; }
        }
        public static int TwitterTweetCapacity
        {
            get { return _twitterTweetCapacity; }
        }
        public static string TwitterScreenName
        {
            get { return _twitterScreenName; }
            set
            {
                _twitterScreenName = value;
                save();
            }
        }
        public static string TwitterPassword
        {
            get { return _twitterPassword; }
            set
            {
                _twitterPassword = value;
                save();
            }
        }
        public static bool TwitterSTTag
        {
            get { return _twitterSTTag; }
            set
            {
                _twitterSTTag = value;
                save();
            }
        }
        public static bool TwitterCustomTagEnabled
        {
            get { return _twitterCustomTagEnabled; }
            set
            {
                _twitterCustomTagEnabled = value;
                save();
            }
        }
        public static string TwitterCustomTag
        {
            get { return _twitterCustomTag; }
            set
            {
                _twitterCustomTag = value;
                save();
            }
        }
        public static bool TwitterCredentialsSet
        {
            get
            {
                if ((_twitterPassword != string.Empty) && (_twitterScreenName != string.Empty))
                {
                    return true;
                }
                else
                {
                    return false;
                }

            }

        }
    }
}
